package com.rech.xbexpress3;

import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.rech.utils.OtherUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.support.constraint.Constraints.TAG;
import static com.zkteco.android.biometric.core.utils.ToolUtils.getApplicationContext;

public class NetworkLogic {
    //private static String Domain = "http://sys.xmxbox.com/api";
    private static String DomainNew = "http://sys.xmxbox.com/api";
    /*private static String Domain = "http://ceshi.xmxbox.com/api"; //测试
    private static String DomainNew = "http://ceshi.xmxbox.com/api"; //测试新*/

    private static String Domain = "http://www.xmxbox.com";

    private static NetworkLogic singletonNetworkLogic = new NetworkLogic();
    //将构造器设置为private禁止通过new进行实例化
    private NetworkLogic() {

    }
    public static NetworkLogic getInstance() {
        return singletonNetworkLogic;
    }
    //查询微信二维码状态
    public void postinfoForWechatState(String tick){
        try{

            JSONObject args = new JSONObject();
            args.put("sn", OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("time",tick);
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/snUserInfo")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();
            Message msg = new Message();
            msg.what = 1200;
            msg.obj =response.body().string();
            EventBus.getDefault().post(msg);

        }catch (Exception e) {
            Message msg = new Message();
            msg.what = 1201;
            EventBus.getDefault().post(msg);
        }

    }

    public void postinfoForReturnOpenGridCmd(String data){
        try{

            JSONObject args = new JSONObject(data);
            args.put("sn", OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("time", System.currentTimeMillis());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder().addInterceptor(new RetryIntercepter(3))
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/REOpenGridCmd")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();
            String resString = response.toString();
            Log.d(TAG, resString);

        } catch (Exception e) {

        }
    }

    public void postinfoForReturnCmd(String cmd,String argbody){
        try{

            JSONObject args = new JSONObject(argbody);
            args.put("sn", OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            args.getJSONObject("data").put("retime",System.currentTimeMillis());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/REOpenGridCmd")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            if(response.code() == 200){
                QueueService.getInstance().remove(cmd);
            }


        }catch (Exception e) {

            //EventBus.getDefault().post(message);
        }
    }


    public void postinfoForheartbeat(){
        try{

            JSONObject args = new JSONObject();
            args.put("sn", OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("time", System.currentTimeMillis());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/cabinetHeart")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message msg = new Message();
            msg.what = 10002;
            msg.obj =response.body().string();
            EventBus.getDefault().post(msg);
        } catch (Exception e) {

            Message msg = new Message();
            msg.what = 10003;
            EventBus.getDefault().post(msg);
        }
    }
    public void postinfoForheartbeatforerr(){
        try{

            JSONObject args = new JSONObject();
            args.put("sn", OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("time", System.currentTimeMillis());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/cabinetHeart")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message msg = new Message();
            msg.what = 10504;
            msg.obj =response.body().string();
            EventBus.getDefault().post(msg);
        } catch (Exception e) {

            Message msg = new Message();
            msg.what = 10005;
            EventBus.getDefault().post(msg);
        }
    }
    //获取微信二维码
    public void postinfoForWechatQR(){

        try{

            JSONObject args = new JSONObject();
            args.put("sn", OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/createQR")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message msg = new Message();
            msg.what = 1100;
            msg.obj =response.body().string();
            EventBus.getDefault().post(msg);
        } catch (Exception e) {

            Message msg = new Message();
            msg.what = 1101;
            EventBus.getDefault().post(msg);
        }

    }

    //寄件码寄件下单
    public void postConfirmOrder(String order_id,int arggridiid,String kdy_id, String wl_id){
        try{
            JSONObject args = new JSONObject();
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("cabinetgridid",arggridiid);
            args.put("orderId",order_id);
            args.put("adminid",kdy_id);
            args.put("cabinetExpId",wl_id);
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DomainNew+"/index/confirmOrder")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=10110;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10111;
            EventBus.getDefault().post(message);
        }
    }

    public void postinfoForJJGridIO485(int box){
        try{
            JSONObject args = new JSONObject();

            args.put("gridType",box);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/faceOpenGrid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=10120;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10121;
            EventBus.getDefault().post(message);
        }
    }

    //获取寄件人、收件人常用地址
    public void postAddress(String mobile, String type){
        try{
            JSONObject args = new JSONObject();

            args.put("mobile",mobile);
            args.put("type",type);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DomainNew+"/index/getUserAddress")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=10129;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10130;
            EventBus.getDefault().post(message);
        }
    }

    //获取快递公司
    public void postLogistics(){
        try{
            JSONObject args = new JSONObject();
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DomainNew+"/index/getLogistics")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=10127;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10128;
            EventBus.getDefault().post(message);
        }
    }

    //获取柜子格子状态
    public void postinfoForBoxState(){
        try{

            JSONObject args = new JSONObject();
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/choiceGrid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=10106;
            message.obj = response.body().string();
            EventBus.getDefault().post(message);
        } catch (Exception e) {
            Message message = new Message();
            message.what=10107;
            EventBus.getDefault().post(message);
        }
    }
    //发货开柜指令v2
    public void postinfoForOpenGridv2(int box,String cabinetgrid){
        try{
            JSONObject args = new JSONObject();

            args.put("gridType",box);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/faceOpenGrid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=10108;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10109;
            EventBus.getDefault().post(message);
        }

    }

    public void postinfoForReOpenGridv2(String cabinetgrid){
        try{
            JSONObject args = new JSONObject();
            args.put("cabinetGridId",cabinetgrid);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/faceREopengrid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=10410;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10411;
            EventBus.getDefault().post(message);
        }

    }

    public void postinfoForBoxBill(String argcardid,String argjjrname,String argjjrphone,int arggridiid,String argljm,String argtoname,String argadminphone,int argexpType, String jjr_address, String sjr_address, String kdy_id, String wl_id){
        try{
            JSONObject args = new JSONObject();
            args.put("cardid",argcardid);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("cabinetGridId",arggridiid);
            args.put("adminPhone",argadminphone);
            args.put("toname",argtoname);
            args.put("exp_type",argexpType);
            args.put("packageid",argljm);
            //args.put("name",argjjrname);
            //args.put("phone",argjjrphone);
            args.put("sender",jjr_address);
            args.put("receiver",sjr_address);
            args.put("adminid",kdy_id);
            args.put("cabinetExpId",wl_id);
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DomainNew+"/index/userCabinetOrder")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=10110;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10111;
            EventBus.getDefault().post(message);
        }
    }
    public void getCabinetType(String id){
        try{
            JSONObject args = new JSONObject();
            args.put("sn",id);
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .addInterceptor(new RetryIntercepter(3))
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DomainNew+"/index/getCabinettype")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=10000;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10001;
            EventBus.getDefault().post(message);
        }
    }
    public void postinfoForBoxType(){
        try{
            JSONObject args = new JSONObject();
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/getCabinettype")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=1500;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=1501;
            EventBus.getDefault().post(message);
        }
    }

    //发货指令
    public void postinfoForFaceCheck(String bitmap,String sfz,String name){
        try{
            JSONObject args = new JSONObject();
            args.put("cardid",sfz);
            args.put("name",name);
            args.put("img",bitmap);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/faceInfo")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=1600;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=1601;
            EventBus.getDefault().post(message);
        }
    }
    //收货指令
    public void postqjmForUserRecvGrid(String qjm) {
        try {
            JSONObject args = new JSONObject();
            args.put("code",qjm);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DomainNew+"/index/getGrid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();
            Message message = new Message();
            message.what=10200;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10201;
            EventBus.getDefault().post(message);
        }
    }

    //获取取件二维码
    public void postScanQj() {
        try {
            JSONObject args = new JSONObject();
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            args.put("type","base64");
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DomainNew+"/index/getRealtimeQrcode")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();
            Message message = new Message();
            message.what=10207;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10208;
            EventBus.getDefault().post(message);
        }
    }

    //寄件码获取寄件信息
    public void postjjmForUserRecvGrid(String jjm) {
        try {
            JSONObject args = new JSONObject();
            args.put("expCode",jjm);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DomainNew+"/index/getOrderInfo")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();
            Message message = new Message();
            message.what=10204;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10205;
            EventBus.getDefault().post(message);
        }
    }


    //从服务器下载微信二维码图片
    public Bitmap getStaticFromHttp(String argUrl){
        Bitmap mIcon11 = null;
        try {

            final OkHttpClient client = new OkHttpClient();

            Request request = new Request.Builder()
                    .url(argUrl)
                    .build();

            Response response = null;

            try {
                response = client.newCall(request).execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (response.isSuccessful()) {
                try {
                    mIcon11 = BitmapFactory.decodeStream(response.body().byteStream());
                } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
            }
            return mIcon11;
        }catch (Exception e) {
        }
        return mIcon11;
    }
    //使服务器生成二维码
    public void getQrCodeForSendGrid(){
        try {
            String tick= String.valueOf(System.currentTimeMillis());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            Request request = new Request.Builder()
                    .url(Domain+"/index/getGrid/"+tick)
                    .build();

            Response response = client.newCall(request).execute();
            Message message = new Message();
            message.what=1900;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=1901;
            EventBus.getDefault().post(message);
        }
    }
    public void postForUserInfo(String cardid,int type,String mobile){
        try{
            JSONObject args = new JSONObject();
            args.put("cardid",cardid);
            args.put("mobile",mobile);
            args.put("type",type);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/getUser")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            if(type == 2){//用户
                Message message = new Message();
                if (!mobile.equals("")) {
                    message.what = 10214;
                } else {
                    message.what=10114;
                }
                message.obj=response.body().string();
                EventBus.getDefault().post(message);
            }else{
                Message message = new Message();
                message.what=10414;
                message.obj=response.body().string();
                EventBus.getDefault().post(message);
            }


        }catch (Exception e) {
            if(type == 2){//用户
                Message message = new Message();
                message.what=10115;
                EventBus.getDefault().post(message);
            }else{
                Message message = new Message();
                message.what=10415;
                EventBus.getDefault().post(message);
            }

        }
    }
    public void postForKDLogin(String user,String pass){
        try{
            JSONObject args = new JSONObject();
            args.put("mobile",user);
            args.put("password",pass);
            args.put("app",2);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/login")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=10400;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10401;
            EventBus.getDefault().post(message);
        }
    }
    public void postForKDLjmList(String id){
        try{
            JSONObject args = new JSONObject();
            args.put("id",id);
            //args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/jiInfo")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=10409;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10410;
            EventBus.getDefault().post(message);
        }
    }
    public void postForKDLjm(String ljm,String sn){
        try{
            JSONObject args = new JSONObject();
            args.put("sn",sn);
            args.put("expressPassword",ljm);

            //args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/jiInfo")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=10402;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10403;
            EventBus.getDefault().post(message);
        }
    }
    public void postYZMForKdReg(String phone) {
        try{
            JSONObject args = new JSONObject();
            args.put("phone",phone);

            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/sendMgs")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=10418;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10419;
            EventBus.getDefault().post(message);
        }
    }
    public void postYZMForJj(String phone) {
        try{
            JSONObject args = new JSONObject();
            args.put("phone",phone);

            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/sendMgs")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=10100;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10101;
            EventBus.getDefault().post(message);
        }
    }
    public void postYZMCheckForKdReg(String cardid,String name,String phone,String pass,String yzm) {
        try{
            JSONObject args = new JSONObject();
            args.put("phone",phone);
            args.put("name",name);
            args.put("cardid",cardid);
            args.put("code",yzm);
            args.put("password",pass);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            args.put("type",1);
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/checkMgs")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=10420;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10421;
            EventBus.getDefault().post(message);
        }
    }
    public void postYZMCheckForUpdate(String cardid,String name,String phone,String yzm) {
        try{
            JSONObject args = new JSONObject();
            args.put("phone",phone);
            args.put("name",name);
            args.put("cardid",cardid);
            args.put("code",yzm);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/editCheckmgs")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=10116;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10117;
            EventBus.getDefault().post(message);
        }
    }
    public void postYZMCheckForJj(String cardid,String name,String phone) {
        try{
            JSONObject args = new JSONObject();
            args.put("phone",phone);
            args.put("name",name);
            args.put("cardid",cardid);
            args.put("type",2);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/checkMgs")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=10116;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10117;
            EventBus.getDefault().post(message);
        }
    }
    public void postinfoForReCmd(String cmd){

    }
    public void postinfoForOrderList(String phone) {
        try{
            JSONObject args = new JSONObject();
            args.put("adminphone",phone);

            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/lanlist")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=10408;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10409;
            EventBus.getDefault().post(message);
        }
    }

    public void postForKDPayOrder(String orderid,String ydh) {
        try{
            JSONObject args = new JSONObject();
            args.put("id",orderid);
            args.put("express_order",ydh);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DomainNew+"/index/lanpayOpengrid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=10406;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10407;
            EventBus.getDefault().post(message);
        }
    }

    public void postUpdateEXPorder(String orderid,String ydh, String freight) {
        try{
            JSONObject args = new JSONObject();
            args.put("id",orderid);
            args.put("express_order",ydh);
            args.put("freight",freight);
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            args.put("ver",OtherUtil.getInstance().getAppVersion());
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/updateEXPorder")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();

            Message message = new Message();
            message.what=10501;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=10502;
            EventBus.getDefault().post(message);
        }
    }

    public void chechWxScan(String tick){
        new FetchQRStateTask(tick).execute();
    }

    public void postCabinetError(String cardid, int type) {
    }

    public void postForexpressId(String expressId) {
        try{
            JSONObject args = new JSONObject();
            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
            //args.put("sn","KD20070002");
            //args.put("express","SF1026577711832");
            args.put("express",expressId);
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DomainNew+"/index/redOpenGrid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();
            Message message = new Message();
            message.what=5001;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

            /*Looper.prepare();
            String json = response.body().string();
            JSONObject jsonObject = new JSONObject(json);
                        if (jsonObject.has("code")) {
                            String code = jsonObject.getString("code");
                            if (TextUtils.equals(code, "200")) {
                                String msg = jsonObject.getString("msg");
                                Message message = new Message();
                                message.what=5004;
                                message.obj=msg;
                                EventBus.getDefault().post(message);

                            } else if (TextUtils.equals(code, "100")) {
                                Message message = new Message();
                                message.what=5001;
                                message.obj=json;
                                EventBus.getDefault().post(message);
                            }
                        }
            Looper.loop();*/
        }catch (Exception e) {
            Toast.makeText(ExpressApp.getInstance(), "开柜异常：" + e.toString(), Toast.LENGTH_LONG).show();
        }
    }

    public void postConfirmByExpress(String expressId, String cabinetgridid) {
        try{
            JSONObject args = new JSONObject();
            args.put("cabinetGridId",cabinetgridid);
            args.put("express",expressId);
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DomainNew+"/index/redConfirm")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();
            Message message = new Message();
            message.what=5002;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);
        }catch (Exception e) {
            Toast.makeText(ExpressApp.getInstance(), "确认异常：" + e.toString(), Toast.LENGTH_LONG).show();
        }
    }


    public void postOrderInfoByExpress(String expressId) {
        try{
            JSONObject args = new JSONObject();
            args.put("express",expressId);
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(Domain+"/index/redOrderInfo")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();
            Message message = new Message();
            message.what=5003;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);
        }catch (Exception e) {
            Toast.makeText(ExpressApp.getInstance(), "获取订单信息异常：" + e.toString(), Toast.LENGTH_LONG).show();
        }
    }
}

class RetryIntercepter implements Interceptor {

    public int maxRetry;//最大重试次数
    private int retryNum = 0;//假如设置为3次重试的话，则最大可能请求4次（默认1次+3次重试）

    public RetryIntercepter(int maxRetry) {
        this.maxRetry = maxRetry;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        System.out.println("retryNum=" + retryNum);
        Response response = chain.proceed(request);
        while (!response.isSuccessful() && retryNum < maxRetry) {
            retryNum++;
            System.out.println("retryNum=" + retryNum);
            response = chain.proceed(request);
        }
        return response;
    }
}


class FetchUserRecvGridTask extends AsyncTask<Void,Void,Void> {
    String qjm;

    public FetchUserRecvGridTask(String argqjm) {
        this.qjm = argqjm;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postqjmForUserRecvGrid(qjm);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchScanQjTask extends AsyncTask<Void,Void,Void> {

    public FetchScanQjTask() {
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postScanQj();
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class PostByCodeTask extends AsyncTask<Void,Void,Void> {
    String jjm;

    public PostByCodeTask(String argjjm) {
        this.jjm = argjjm;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postjjmForUserRecvGrid(jjm);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchQRStateTask extends AsyncTask<Void,Void,Void> {

    String tick;
    public FetchQRStateTask(String argtick) {
        this.tick = argtick;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postinfoForWechatState(tick);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchBillTask extends AsyncTask<Void,Void,Void> {

    String cardid;
    String jjrname;
    String jjrphone;
    int gridiid;
    String ljm;
    String toname;
    String adminphone;
    int expType;
    String jjr_address;
    String sjr_address;
    String kdy_id;
    String wl_id;

    public FetchBillTask(String argcardid,String argjjrname,String argjjrphone,int arggridiid,String argljm,String argtoname,String argadminphone,int argexpType, String jjr_address, String sjr_address, String kdy_id, String wl_id){
        this.cardid = argcardid;
        this.jjrname = argjjrname;
        this.jjrphone = argjjrphone;
        this.gridiid = arggridiid;
        this.ljm = argljm;
        this.toname = argtoname;
        this.adminphone = argadminphone;
        this.expType =  argexpType;
        this.jjr_address =  jjr_address;
        this.sjr_address =  sjr_address;
        this.kdy_id =  kdy_id;
        this.wl_id =  wl_id;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postinfoForBoxBill(cardid,jjrname,jjrphone,gridiid,ljm,toname,adminphone,expType, jjr_address,sjr_address,kdy_id,wl_id);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchConfirmOrder extends AsyncTask<Void,Void,Void> {

    String order_id;
    int gridiid;
    String kdy_id;
    String wl_id;

    public FetchConfirmOrder(String order_id,int arggridiid,String kdy_id, String wl_id){
      this.gridiid = arggridiid;
        this.kdy_id =  kdy_id;
        this.order_id =  order_id;
        this.wl_id =  wl_id;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postConfirmOrder(order_id,gridiid,kdy_id,wl_id);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}



class FetchOrderListTask extends AsyncTask<Void, Void, Void> {
    String phone;
    public FetchOrderListTask(String argphone) {
        this.phone = argphone;
    }

    protected Void doInBackground(Void... params) {
        NetworkLogic.getInstance().postinfoForOrderList(phone);
        return null;
    }

    protected void onPostExecute(Void result) {

    }
}
//﻿{"code":"100","msg":{"type":"CMD_JJ_OPEN_GRID","cabinetCode":"KD20070001","gridNum":"1","IO":1,"485":1,"time":1592291074}}
class FetchIO485GridTaskForJJ extends AsyncTask<Void, Void, Void> {
    int boxtype;
    public FetchIO485GridTaskForJJ(int argboxtype) {
        this.boxtype = argboxtype;
    }

    protected Void doInBackground(Void... params) {
        NetworkLogic.getInstance().postinfoForJJGridIO485(boxtype);
        return null;
    }

    protected void onPostExecute(Void result) {

    }
}

class FetchAddress extends AsyncTask<Void, Void, Void> {
    String mobile;
    String type;
    public FetchAddress(String mobile, String type) {
        this.mobile = mobile;
        this.type = type;
    }

    protected Void doInBackground(Void... params) {
        NetworkLogic.getInstance().postAddress(mobile, type);
        return null;
    }

    protected void onPostExecute(Void result) {

    }
}

class FetchLogistics extends AsyncTask<Void, Void, Void> {
    public FetchLogistics() {
    }

    protected Void doInBackground(Void... params) {
        NetworkLogic.getInstance().postLogistics();
        return null;
    }

    protected void onPostExecute(Void result) {

    }
}

class FetchgetCabinetType extends AsyncTask<Void,Void,Void> {

    String cabinet;
    public FetchgetCabinetType(String argcabinet) {
        this.cabinet = argcabinet;
    }
    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().getCabinetType(cabinet);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}
class FetchUserInfoTask extends AsyncTask<Void,Void,Void>{
    String cardid;
    String mobile;
    int type;

    public FetchUserInfoTask(String argcardid,int argtype, String mobile) {
        this.cardid = argcardid;
        this.type = argtype;
        this.mobile = mobile;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postForUserInfo(cardid,type,mobile);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}
class FetchUrlImageTask extends AsyncTask<Void, Void, Bitmap> {
    String urlImage;
    ImageView ctrl;

    public FetchUrlImageTask(String argurl,ImageView argctrl) {
        this.urlImage = argurl;
        this.ctrl = argctrl;
    }

    protected Bitmap doInBackground(Void... urls) {


        return NetworkLogic.getInstance().getStaticFromHttp(urlImage);
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        //initBoxResultFormHttp(true,true,true);
        ctrl.setImageBitmap(result);
    }
}

class FetchUrlImageToBase64Task extends AsyncTask<Void, Void, Bitmap> {
    String urlImage;
    String key;

    public FetchUrlImageToBase64Task(String argurl,String argkey) {
        this.urlImage = argurl;
        this.key = argkey;
    }

    protected Bitmap doInBackground(Void... urls) {


        return NetworkLogic.getInstance().getStaticFromHttp(urlImage);
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        //initBoxResultFormHttp(true,true,true);
        String value = OtherUtil.bitmapToBase64(result);
        OtherUtil.setValue(key,value);
    }
}
class FetchGetBoxStateTask extends AsyncTask<Void,Void,Void> {
    @Override
    protected Void doInBackground(Void... params) {
        NetworkLogic.getInstance().postinfoForBoxState();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        //initBoxResultFormHttp(true,true,true);
    }
}

class FetchKDLoginTask extends AsyncTask<Void,Void,Void>{
    String username;
    String password;

    public FetchKDLoginTask(String arguser,String argpass) {
        this.username = arguser;
        this.password = argpass;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postForKDLogin(username,password);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchKDLjmListTask extends AsyncTask<Void,Void,Void>{

    String id;


    public FetchKDLjmListTask(String argid) {

        this.id = argid;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postForKDLjmList(id);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}


class FetchKDLjmTask extends AsyncTask<Void,Void,Void>{
    String ljm;
    String sn;

    public FetchKDLjmTask(String argljm,String argsn) {
        this.ljm = argljm;
        this.sn = argsn;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postForKDLjm(ljm,sn);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}
class FetchYZMTask extends AsyncTask<Void,Void,Void> {
    String phone;

    public FetchYZMTask(String argphone) {
        this.phone = argphone;
    }
    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postYZMForJj(phone);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchYZMTaskForKDReg extends AsyncTask<Void,Void,Void> {
    String phone;

    public FetchYZMTaskForKDReg(String argphone) {
        this.phone = argphone;
    }
    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postYZMForKdReg(phone);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}
class FetchYZMCheckTask extends AsyncTask<Void,Void,Void> {
    String phone;
    String yzm;
    String cardid;
    String name;
    public FetchYZMCheckTask(String argcardid,String argname,String argphone) {
        this.phone = argphone;
        this.cardid = argcardid;
        this.name = argname;
    }
    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postYZMCheckForJj(cardid,name,phone);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}
class FetchYZMCheckForUpdateTask extends AsyncTask<Void,Void,Void> {
    String phone;
    String cardid;
    String name;
    String yzm;
    public FetchYZMCheckForUpdateTask(String argcardid,String argname,String argphone,String argyzm) {
        this.phone = argphone;
        this.cardid = argcardid;
        this.name = argname;
        this.yzm = argyzm;
    }
    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postYZMCheckForUpdate(cardid,name,phone,yzm);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchYZMCheckTaskForKdReg extends AsyncTask<Void,Void,Void> {
    String phone;
    String cardid;
    String name;
    String pass;
    String yzm;
    public FetchYZMCheckTaskForKdReg(String argcardid,String argname,String argphone,String argpass,String argyzm) {
        this.phone = argphone;
        this.cardid = argcardid;
        this.name = argname;
        this.pass = argpass;
        this.yzm = argyzm;
    }
    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postYZMCheckForKdReg(cardid,name,phone,pass,yzm);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}
class FetchKDPayOrderTask extends AsyncTask<Void,Void,Void> {
    String ydh;
    String orderid;
    public FetchKDPayOrderTask(String argorder,String argydh) {
        this.ydh = argydh;
        this.orderid = argorder;
    }
    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postForKDPayOrder(orderid,ydh);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchUpdateEXPorderTask extends AsyncTask<Void,Void,Void> {
    String ydh;
    String orderid;
    String fei;
    public FetchUpdateEXPorderTask(String argorder,String argydh, String fei) {
        this.ydh = argydh;
        this.orderid = argorder;
        this.fei = fei;
    }
    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postUpdateEXPorder(orderid,ydh,fei);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchHeartBeatTask extends AsyncTask<Void,Void,Void> {


    public FetchHeartBeatTask() {

    }
    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postinfoForheartbeat();
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}
class FetchReturnCmdTask extends AsyncTask<Void,Void,Void> {

    String json;
    String cmd;

    public FetchReturnCmdTask(String argcmd,String argjson){
        this.cmd = argcmd;
        this.json = argjson;

    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postinfoForReturnCmd(cmd,json);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}
class FetchHeartBeatTaskForError extends AsyncTask<Void,Void,Void> {


    public FetchHeartBeatTaskForError() {

    }
    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postinfoForheartbeat();
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}
class FetchReturnOpenGridCmdTask extends AsyncTask<Void,Void,Void> {
    String data;

    public FetchReturnOpenGridCmdTask(String arg) {
        data = arg;
    }
    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postinfoForReturnOpenGridCmd(data);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class UploadCabinetErrorTask extends AsyncTask<Void,Void,Void>{
    String cardid;
    int type;

    public UploadCabinetErrorTask(String argcardid,int argtype) {
        this.cardid = argcardid;
        this.type = argtype;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postCabinetError(cardid,type);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }

}

class OpenCabinetByExpressTask extends AsyncTask<Void,Void,Void>{

    String expressId;


    public OpenCabinetByExpressTask(String expressId) {

        this.expressId = expressId;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postForexpressId(expressId);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class ConfirmByExpressTask extends AsyncTask<Void,Void,Void>{

    String expressId;
    String cabinetgridid;


    public ConfirmByExpressTask(String expressId, String cabinetgridid) {

        this.expressId = expressId;
        this.cabinetgridid = cabinetgridid;

    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postConfirmByExpress(expressId, cabinetgridid);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class PostOrderInfoByExpressTask extends AsyncTask<Void,Void,Void>{

    String expressId;


    public PostOrderInfoByExpressTask(String expressId) {

        this.expressId = expressId;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postOrderInfoByExpress(expressId);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}